package com.java.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.dao.ActivityRecordDao;
import com.java.dao.ActivitySlotDao;
import com.java.domain.ActivityRecord;
import com.java.domain.event.ActivityRecordEvent;
import com.java.domain.event.ActivitySlotEvent;
import com.java.service.RecordService;
import com.java.service.SlotService;
import com.java.service.SportService;
import com.java.service.UserService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RecordServiceImpl implements RecordService {

	@Resource ActivityRecordDao recordDao;
	@Resource ActivitySlotDao slotDao;
	@Autowired private SlotService slotService;
	@Autowired private SportService sportService;
	@Autowired private UserService userService;

	@Override
	public TableRespone getList(String userId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("recordUserId", userId);
		List<ActivityRecordEvent> list = recordDao.selectAll(params, page, size);
		plugRecord(list);
		long total = recordDao.selectAllCount(params);
		return new TableRespone(list, total);
	}
	
	private void plugRecord(List<ActivityRecordEvent> list) {
		if(null != list) {
			for (ActivityRecordEvent record : list) {
				record.setSport(sportService.getOne(record.getRecordSportId()));
				record.setSlot(slotService.getOne(record.getRecordSlotId()));
				record.setUser(userService.getUserById(record.getRecordUserId()));
			}
		}
	}

	@Override
	public ActivityRecordEvent getOne(String recordId) {
		ActivityRecordEvent info = recordDao.selectByKey(recordId);
		return info;
	}

	@Override
	public BaseRespone updateRecord(ActivityRecord record) {
		// check date for slot
		ActivityRecord info = recordDao.selectByKey(record.getRecordId());
		info.setRecordDate(record.getRecordDate());
		info.setRecordPrice(record.getRecordPrice());
		int row = recordDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone delRecord(String recordId) {
		int row = recordDao.deleteByKey(recordId);
		return BaseRespone.result(row, "Delete Success");
	}

	@Override
	public BaseRespone createOrder(String sportId, String slotId, String userId, String date) {
		ActivityRecord record = recordDao.selectAllByUser(sportId, slotId, userId, date);
		if(null != record) {
			return BaseRespone.error("Not Repeat");
		}
		ActivitySlotEvent slotInfo = slotDao.selectByKey(slotId);
		record = new ActivityRecord();
		record.setRecordId(MyTools.getUUID());
		record.setRecordSportId(sportId);
		record.setRecordSlotId(slotId);
		record.setRecordUserId(userId);
		record.setRecordPrice(slotInfo.getSlotPrice());
		record.setRecordDate(MyTools.getDate(date));
		record.setRecordTime(new Date());
		record.setRecordPay(0);
		record.setRecordConfirm(0);
		int row = recordDao.insertSelective(record);
		return BaseRespone.result(row, "Create Success");
	}

	@Override
	public BaseRespone payRecord(String recordId, int payStatus) {
		ActivityRecord record = new ActivityRecord();
		record.setRecordId(recordId);
		record.setRecordPay(payStatus);
		if(payStatus == 1) {
			record.setRecordPayTime(new Date());
		}
		int row = recordDao.updateSelective(record);
		return BaseRespone.result(row, "Pay Success");
	}

	@Override
	public BaseRespone confirmRecord(String recordId, int confirmStatus) {
		ActivityRecord record = new ActivityRecord();
		record.setRecordId(recordId);
		record.setRecordConfirm(confirmStatus);
		if(confirmStatus == 1) {
			record.setRecordConfirmTime(new Date());
		}
		int row = recordDao.updateSelective(record);
		return BaseRespone.result(row, "Confirm Success");
	}

	@Override
	public BigDecimal getAmountOfDay(String dateStr) {
		return recordDao.selectDateAmount(dateStr);
	}

}
